package com.glory.wxmemsicpda.activity

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import com.glory.wxmemsicpda.bean.EqpInfoBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.JsonObject
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils
import kotlinx.android.synthetic.main.activity_login.*

class SkipDialogActivity : FreeUI_BaseActivity<Any?>() {
    private var skipDialog: FreeUI_GeneralFragmentDialog? = null
    override fun getLayoutResId(): Int {
        return 0
    }

    override fun initView() {
        val extra = intent.getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY)
        when {
            extra!!.contains("Scan_Start") -> {
                skipDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描EQPID") {
                    FreeApi_Utils.skipWithExtraIntent(
                        it,
                        this@SkipDialogActivity,
                        WorkBeginActivity::class.java
                    )
                    skipDialog!!.dismiss()
                }
                skipDialog!!.setCallBack {
                    this@SkipDialogActivity.finish()
                }
                skipDialog!!.show(supportFragmentManager, "Scan_Start")
            }
            extra.contains("Scan_Back") -> {
                skipDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描EQPID") {
                    FreeApi_Utils.skipWithExtraIntent(
                        it,
                        this@SkipDialogActivity,
                        AbortActivity::class.java
                    )
                    skipDialog!!.dismiss()
                }
                skipDialog!!.setCallBack {
                    this@SkipDialogActivity.finish()
                }
                skipDialog!!.show(supportFragmentManager, "Scan_Back")
            }
            extra.contains("Scan_End_Package") -> {
                skipDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描EQPID") {
                    //region 测试
//                    FreeApi_Utils.skipWithExtraIntent(
//                        it,
//                        this@SkipDialogActivity,
//                        WorkEndPackageActivity::class.java
//                    )
                    //endregion
                    //请求设备信息
                    requestEqp(it)

                }
                skipDialog!!.setCallBack {
                    this@SkipDialogActivity.finish()
                }
                skipDialog!!.show(supportFragmentManager, "Scan_End_Package")
            }
            extra.contains("Scan_End_Test") -> {
                skipDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描EQPID") {
                    FreeApi_Utils.skipWithExtraIntent(
                        it,
                        this@SkipDialogActivity,
                        WorkEndTestActivity::class.java
                    )
                    skipDialog!!.dismiss()
                }
                skipDialog!!.setCallBack {
                    this@SkipDialogActivity.finish()
                }
                skipDialog!!.show(supportFragmentManager, "Scan_End_Test")
            }
            extra.contains("Check_Search") -> {

                val intent = Intent(this, CheckSearchActivity::class.java)
                startActivity(intent)

                }
            extra.contains("Check_Inspection") -> {

                val intent = Intent(this, CheckInspectionActivity::class.java)
                startActivity(intent)

                }
            extra.contains("Check_Table") -> {

                val intent = Intent(this, CheckTableActivity::class.java)
                startActivity(intent)

                }

            extra.contains("Scan_Change_Materials") -> {
                skipDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描EQPID") {
                    FreeApi_Utils.skipWithExtraIntent(
                        it,
                        this@SkipDialogActivity,
                        MaterialsListActivity::class.java
                    )
                    skipDialog!!.dismiss()
                }
                skipDialog!!.setCallBack {
                    this@SkipDialogActivity.finish()
                }
                skipDialog!!.show(supportFragmentManager, "Scan_Change_Materials")
            }

            extra.contains("Data_Acquisition") -> {
                skipDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描EQPID") {
                    FreeApi_Utils.skipWithExtraIntent(
                        it,
                        this@SkipDialogActivity,
                        DataAcquisitionActivity::class.java
                    )
                    skipDialog!!.dismiss()
                }
                skipDialog!!.setCallBack {
                    this@SkipDialogActivity.finish()
                }
                skipDialog!!.show(supportFragmentManager, "Data_Acquisition")
            }

            extra.contains("Feeding_Operations") -> {

//                val intent = Intent(this, FGFeedingOperationsActivity::class.java)
//                startActivity(intent)
                FreeApi_Utils.skipWithExtraIntent(
                    "",
                    this@SkipDialogActivity,
                    FGFeedingOperationsActivity::class.java
                )

                finish()

            }

        }
    }

    //批次结束，获取设备信息
    private fun requestEqp(eqp: String){
//        val ss = "{\"Response\":{\"Header\":{\"TRANSACTIONID\":\"c5e5e318389d4c39b914e4c10ba519d4\",\"RESULT\":\"SUCCESS\",\"RESULTCODE\":null,\"RESULTMESSAGE\":null},\"Body\":{\"EQP_ID\":\"MO-001\",\"LOT_ID\":\"TS00001\",\"INPUT_TYPE\":\"0\",\"START_TIME\":\"2021-01-29T16:34:24\",\"END_TIME\":\"2021-01-29T16:34:24\",\"TOTAL_QUANTITY\":\"2100\",\"TOTAL_GOOD_QUANTITY\":\"0\",\"TOTAL_FAIL_QUANTITY\":\"0\",\"TESTER_PASS_QUANTITY\":\"0\",\"TESTER_FAIL_QUANTITY\":\"0\",\"BIN_LIST\":[{\"PNAME\":\"BIN0\",\"PVALUE\":\"1\"},{\"PNAME\":\"BIN1\",\"PVALUE\":\"1\"}],\"DEFECT_CODE_LIST\":[{\"REJECTS_CODE\":\"4KUAISU1\",\"REJECTS_QUANTITY\":\"1\"},{\"REJECTS_CODE\":\"5KUAISU2\",\"REJECTS_QUANTITY\":\"1\"}]}}}";
//        var dd : BaseResponseBean<EqpInfoBean> = Gson().fromJson(ss,object : TypeToken<BaseResponseBean<EqpInfoBean>>(){}.type)
//        if (dd!!.isSuccess(false, supportFragmentManager)) {
//            val entity = dd!!.response.body
//            val bundle = Bundle()
//            bundle.putSerializable("Entity",entity)
//            when {
//                entity?.inpuT_TYPE.equals("0") -> {
//                    FreeApi_Utils.skipWithExtraIntent(
//                        "header",
//                        bundle,
//                        this@SkipDialogActivity,
//                        WorkEndPackageActivity::class.java
//                    )
//                }
//                entity?.inpuT_TYPE.equals("1") -> {
//                    FreeApi_Utils.skipWithExtraIntent(
//                        "header",
//                        bundle,
//                        this@SkipDialogActivity,
//                        WorkEndTestActivity::class.java
//                    )
//                }
//                else -> {
//                    showToast("服务器传值错误",false)
//                }
//            }
//        }
        val map = JsonObject()
        map.addProperty("EQP_ID", eqp)
        NetUtils.requestPosNet(
            this,
            "PDALotEndInfo",
            "PDALotEndInfo",
            map,
            object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<EqpInfoBean?>?>(
                this
            ) {
                override fun onSuccess(response: Response<BaseResponseBean<EqpInfoBean?>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.isSuccess(false, supportFragmentManager)) {
                        val entity = response.body()!!.response.body
                        val bundle = Bundle()
                        bundle.putSerializable("Entity", entity)
                        when {
                            entity?.inpuT_TYPE.equals("0") -> {
                                FreeApi_Utils.skipWithExtraIntent(
                                    "header",
                                    bundle,
                                    this@SkipDialogActivity,
                                    WorkEndPackageActivity::class.java
                                )
                                finish()
                            }
                            entity?.inpuT_TYPE.equals("1") -> {
                                FreeApi_Utils.skipWithExtraIntent(
                                    "header",
                                    bundle,
                                    this@SkipDialogActivity,
                                    WorkEndTestActivity::class.java
                                )
                                finish()
                            }
                            else -> {
                                showToast("服务器传值错误", false)
                                Handler().postDelayed({ finish() }, 2000)
                            }
                        }
                    }else{
                        Handler().postDelayed({ finish() }, 2000)
                    }
                }

                override fun onError(response: Response<BaseResponseBean<EqpInfoBean?>?>) {
                    super.onError(response)
                    loadError(response.exception, "PDALotEndInfo")
                }

            })
    }

    override fun initData() {}
    override fun getBaseEntity(): FreeUI_AddViewEntity? {
        return null
    }


}